#ifndef FH_INIFILE_H
#define FH_INIFILE_H 1

#include <string>
#include <map>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <ctype.h>
#include <algorithm>
using namespace std;

namespace HttpFrame
{

typedef map<string, string, less<string> > strMap;
typedef strMap::iterator strMapIt;
//typedef map<string,map<string> > ConfigContainor;

inline void ToLower(string &str)
{
	if(str.empty())
	{
		return ;
	}
	for(string::size_type idx = 0;idx < str.size();idx++)
	{
		str[idx]=tolower(str[idx]);
	}
	return ;
}

const char*const MIDDLESTRING = "_____***_______";

struct analyzeini{
    string strsect;
    strMap *pmap;
    analyzeini(strMap & strmap):pmap(&strmap){}
    void operator()( const string & strini)
    {
        string::size_type first =strini.find('[');
        string::size_type last = strini.rfind(']');
        if( first != string::npos && last != string::npos && first != last+1)
        {
            strsect = strini.substr(first+1,last-first-1);
            return ;
        }
        if(strsect.empty())
            return ;
        if((first=strini.find('='))== string::npos)
            return ;
        string strtmp1= strini.substr(0,first);
        string strtmp2=strini.substr(first+1, string::npos);
        first= strtmp1.find_first_not_of(" \t");
        last = strtmp1.find_last_not_of(" \t");
        if(first == string::npos || last == string::npos)
            return ;
        string strkey = strtmp1.substr(first, last-first+1);
        first = strtmp2.find_first_not_of(" \t");
        if(((last = strtmp2.find("\t#", first )) != string::npos) ||
            ((last = strtmp2.find(" #", first )) != string::npos) ||
            ((last = strtmp2.find("\t//", first )) != string::npos)||
            ((last = strtmp2.find(" //", first )) != string::npos))
        {
            strtmp2 = strtmp2.substr(0, last-first);
        }
        last = strtmp2.find_last_not_of(" \t");
        if(first == string::npos || last == string::npos)
            return ;
        string value = strtmp2.substr(first, last-first+1);
        string mapkey = strsect + MIDDLESTRING;
        mapkey += strkey;
        ToLower(mapkey);
        (*pmap)[mapkey]=value;
        return ;
    }
};


class CIniFile
{
public:
    CIniFile( ){};
    ~CIniFile( ){};
    bool Open(const char* pinipath)
    {
        m_strPath = pinipath;
        return do_open(pinipath);
    }

    const char *Path(){ return m_strPath.c_str(); }
    
    string Read(const char*psect, const char*pkey)
    {
        string mapkey = psect;
        mapkey += MIDDLESTRING;
        mapkey += pkey;
        ToLower(mapkey);
        strMapIt it = c_inimap.find(mapkey);
        if(it == c_inimap.end())
            return "";
        else
            return it->second;
    }
    string GetString(const char*psect, const char*pkey,std::string defValue="")
    {
    	string strTmp;
    	strTmp = Read(psect,pkey);
    	if(strTmp=="")
    	{
    		return defValue;
    	}
    	return strTmp;
    }
    int GetInt(const char*psect, const char*pkey,int nDefValue=0)
    {
    	string strTmp;
    	strTmp = Read(psect,pkey);
    	if(strTmp=="")
    	{
    		return nDefValue;
    	}
    	return atoi(strTmp.c_str());
    }
    unsigned int GetUint(const char*psect, const char*pkey,unsigned int nDefValue=0)
    {
    	string strTmp;
    	strTmp = Read(psect,pkey);
    	if(strTmp=="")
    	{
    		return nDefValue;
    	}
    	return (unsigned int)strtoul(strTmp.c_str(), NULL, 10);
    }
    long GetLong(const char*psect, const char*pkey,long lDefValue=0)
    {
    	string strTmp;
    	strTmp = Read(psect,pkey);
    	if(strTmp=="")
    	{
    		return lDefValue;
    	}
    	return atol(strTmp.c_str());
    }
    unsigned long  GetUlong(const char*psect, const char*pkey,unsigned long lDefValue=0)
    {
    	string strTmp;
    	strTmp = Read(psect,pkey);
    	if(strTmp=="")
    	{
    		return lDefValue;
    	}
    	return (unsigned long)atol(strTmp.c_str());
    }
    
protected:
    bool do_open(const char* pinipath)
    {
        ifstream fin(pinipath);
        if(!fin.is_open())
            return false;
        vector<string> strvect;
        while(!fin.eof())
        {
            string inbuf;
            getline(fin, inbuf,'\n');
            //ToLower(inbuf);
            strvect.push_back(inbuf);
        }
        if(strvect.empty())
            return false;
        for_each(strvect.begin(), strvect.end(), analyzeini(c_inimap));
        return !c_inimap.empty();
    }
    strMap c_inimap;
    string   m_strPath;
    //ConfigContainor
};


};

#endif

